<template>
  <!-- 添加修改对话框 -->
  <el-dialog
    v-model="isShow"
    :title="`${title}用户`"
    width="700"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px" class="user-dialog-form">
      <el-form-item label="用户名:" prop="username">
        <el-input :disabled="title == '编辑'" v-model="form.username" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="form.password" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="手机号:" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="年龄:" prop="age">
        <el-input v-model="form.age" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="性别:" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择" clearable>
          <el-option v-for="sex in sexOptions" :key="sex.id" :label="sex.label" :value="sex.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="角色:" prop="role">
        <el-select v-model="form.role" placeholder="请选择" clearable>
          <el-option v-for="role in roleOptions" :key="role.id" :label="role.label" :value="role.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="头像:" prop="photo">
        <el-input v-model="form.photo" type="textarea" :rows="4" placeholder="请输入头像url" clearable />
      </el-form-item>
      <el-form-item v-if="title == '新增'" label="创建日期：" prop="createdAt">
        <el-date-picker
          v-model="form.createdAt"
          type="date"
          placeholder="请选择"
          clearable
        />
      </el-form-item>
      <el-form-item v-else label="修改日期：" prop="updatedAt">
        <el-date-picker
          v-model="form.updatedAt"
          type="date"
          placeholder="请选择"
          clearable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancelHandle">取消</el-button>
        <el-button type="primary" @click="saveHandle">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="user">
import { ref, reactive, toRaw } from "vue";
import { Search, Plus, Delete, Edit, Upload, Pointer } from "@element-plus/icons-vue";
import { useUserStore } from "@/stores/user";
import { useMessage } from "@/hooks/message";
import {getUserListApi,  addUserApi, updateUserApi, delUserApi, getRoleOptionApi, getSexOptionApi} from '@/api/user';

const props = defineProps(['sexOptions', 'roleOptions'])
const emit = defineEmits(['search'])

const message = useMessage();
const formRef = ref(null)
const isShow = ref(false);
let title = ref('');
let form = reactive({
  username: "",
  password: '',
  phone: '',
  age: "",
  sex: "",
  role: "",
  photo: 'https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2025%2F0225%2F55e11aebj00ss7wp00032d000sg00sgg.jpg&thumbnail=660x2147483647&quality=80&type=jpg',
  createdAt: "",
  updatedAt: "",
});
const rules = reactive({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
  ],
  phone: [
    { required: true, message: '手机号不能为空', trigger: 'blur' },
    { required: true, pattern : /^1[3-9]\d{9}$/, message: '手机号格式不对', trigger: ['blur', 'change'] },
  ],
  role: [
    { required: true, message: '角色不能为空', trigger: ['blur', 'change'] },
  ],
  
})


const show = (option = {}) => {
  console.log(option);
  
  isShow.value = true;
  title.value = option.title;
  option.form && (form = option.form)
}

const cancelHandle = () => {
  formRef.value.resetFields()
  isShow.value = false;
};

const saveHandle = async() => {
  await formRef.value.validate((valid, fields) => {
    if (valid) {
      let addOrUpdate;
      if (title.value == '新增') {
        addOrUpdate = addUserApi
        delete form.userid
        delete form._id
      } else {
        addOrUpdate = updateUserApi
      }
      
      addOrUpdate(form).then(res => {
        if (res.code == 200) {
          cancelHandle();
          emit('search')
          message.success(res.msg)
        }else {
          // message.error(res.msg)
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
};

defineExpose({
  show,
})
</script>

<style lang="less" scoped>
.user-dialog-form {
  .el-input {
    --el-input-width: 500px;
  }

  .el-select {
    --el-select-width: 500px;
  }

  .el-textarea {
    width: 500px;
  }
}
</style>
